import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-purchase-apply',
	templateUrl: './purchase-apply.component.html'
})
export class PurchaseApplyComponent implements OnInit {
	fromType: any = '';
	tableData = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	buyApplyType: string = '';
	applyDepartmet: string = ''; //申请部门
	applicant: string = ''; //申请人
	addPermission: boolean = false;
	delPermission: boolean = false;
	editPermission: boolean = false;

	startTime_search: string = '';
	endTime_search: string = '';
	specifyId = {
		picker1: 'datetimepicker1',
		picker2: 'datetimepicker2'
	}
	specifyTime = {
		startTime: '',
		endTime: '',
	}

	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}

	ngOnInit() {
		this.initBtn()
		this.load();
	}

	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.startTime_search = time.startTime;
		this.endTime_search = time.endTime;
	}

	//初始化权限按钮
	initBtn() {
		const btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_supplyChain:purchaseApply')))
		if (!btnData) {
			return
		}
		for (let item of btnData) {
			if (item.sourceKey === 'purchaseApply:addBtn' && item.parentId) {
				this.addPermission = true
			}
			if (item.sourceKey === 'purchaseApply:delBtn' && item.parentId) {
				this.delPermission = true
			}
			if (item.sourceKey === 'purchaseApply:editBtn' && item.parentId) {
				this.editPermission = true
			}
		}
	}
	//table刷新
	refresh() {
		this.load();
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		// this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		const data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				buyApplyType: this.buyApplyType,
				// searchKey: common.stringTrim(this.searchKey),
				startTime: this.startTime_search,
				endTime: this.endTime_search,
				applyDepartmet: this.applyDepartmet,
				applicant: this.applicant
			}
		}
		const reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("dtsBuyApply/page.html", reqdata).subscribe(result => {
			if (result.isSuccess) {
				this.tableData = result.exhibitDatas || [];
				this.totalCount = result.recordCount;
			}
		})
	}
	
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	deleteRow(row) {
		this.jsonApiService.confirm_message('删除采购记录会同步永久删除采购明细，确认要删除?').then(() => {
			const reqdata = 'id=' + row.id;
			this.jsonApiService.http("dtsBuyApply/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	// 跳转明细
	goPurchaseDetail(row) {
		const BaseURL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
		common.setCookie('purchaseDetail', JSON.stringify(row));
		window.open(BaseURL + '#/supply-chain/purchase-detail');
	}
	//新建
	new_purchase() {
		this.router.navigate(['/supply-chain/purchase-apply/purchase-page'], {
			queryParams: {
				type: 'new'
			}
		});
	}
	//编辑页面
	updata_purchase(row) {
		common.setCookie('purchaseUpdata', JSON.stringify(row));
		this.router.navigate(['/supply-chain/purchase-apply/purchase-page'], {
			queryParams: {
				id: row.id,
				type: 'updata',
				nowPage: this.curPage
			}
		});
	}
	// 删除输入
	delKey(type) {
		if (type == "applyDepartmet") {
			this.applyDepartmet = ""
		}
		if (type == "applicant") {
			this.applicant = ""
		}
	}
}